package com.example.wacoffee_manage.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

public class JwtUtil {
    private static final String SECRET_KEY = "czwa";
    private static final long EXPIRATION_TIME = 3600000;

    public static String generateJwt(Map<String, Object> claims){
        String jwt= Jwts.builder()
                .addClaims(claims)
                .signWith(io.jsonwebtoken.SignatureAlgorithm.HS256, SECRET_KEY)
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME))
                .compact();
        return jwt;
    }
    public static Claims parseJwt(String jwt){
        return Jwts.parser()
                .setSigningKey(SECRET_KEY)
                .parseClaimsJws(jwt)
                .getBody();
    }
    public static boolean isTokenExpired(String token) {
        try {
             Jwts.parser()
                    .setSigningKey(SECRET_KEY)
                    .parseClaimsJws(token)
                    .getBody();
        }catch (Exception e){
            return true;
        }
        return false;
    }
}
